package com.dungnt.vtime.event;

//import android.R;
import static android.widget.Toast.LENGTH_SHORT;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.dungnt.vtime.R;
import com.dungnt.vtime.VTCalendarActivity;
import com.dungnt.vtime.utils.Utils;
import com.squareup.timessquare.CalendarPickerView;
import com.squareup.timessquare.CalendarPickerView.SelectionMode;

public class VTEventFragment extends Fragment implements OnClickListener {

	View v;
	private EventUtils eventUtils;
	private Button btnInsert, btnGet, btnClear;
	private ListView lstEvent;
	public static final int REQUEST_CODE_EVENT = 1;
	public static final int REQUEST_CODE_CALENDAR = 2;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		v = inflater.inflate(R.layout.event_fragment, container, false);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		btnGet = (Button) v.findViewById(R.id.event_btnGetImage);
		btnInsert = (Button) v.findViewById(R.id.event_btnInsert);
		btnGet.setOnClickListener(this);
		btnInsert.setOnClickListener(this);
		lstEvent = (ListView) v.findViewById(R.id.event_lstView);
		eventUtils = EventUtils.getEventUtilsInstance(getActivity());
		btnClear = (Button) v.findViewById(R.id.event_clearAll);
		btnClear.setOnClickListener(this);

		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == btnInsert) {
			// for (int i = 0; i < 5; i++) {
			// EventItem e = new EventItem("Happy New Year", "2014",
			// System.currentTimeMillis(), i % 2,
			// Utils.bitmapToByteArray(getActivity(),
			// R.drawable.ic_launcher));
			// eventUtils.insertEventToDB(e);
			// }
			Intent i = new Intent(getActivity(), VTCalendarActivity.class);
			startActivityForResult(i, REQUEST_CODE_CALENDAR);

		} else if (v == btnGet) {
			ArrayList<EventItem> data = eventUtils.getEventsFromDB();
			EventListAdapter eventAdapter = new EventListAdapter(getActivity(),
					data);
			lstEvent.setAdapter(eventAdapter);
		} else if (v == btnClear) {
			getActivity().getContentResolver().delete(
					EventProvider.CONTENT_URI, null, null);
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == REQUEST_CODE_CALENDAR
				&& resultCode == Activity.RESULT_OK) {
			int year = data.getIntExtra("year", 0); // get number of year
			int month = data.getIntExtra("month", 0); // get number of month
			// 0..11
			int day = data.getIntExtra("day", 0); // get number of day 0..31

			// format date and display on screen
			final Calendar dat = Calendar.getInstance();
			dat.set(Calendar.YEAR, year);
			dat.set(Calendar.MONTH, month);
			dat.set(Calendar.DAY_OF_MONTH, day);

			// // show result
			SimpleDateFormat format = new SimpleDateFormat("yyyy MMM dd");
			btnInsert.setText(format.format(dat.getTime()));
		}
	}
}
